#define _CRT_SECURE_NO_WARNINGS 1

#include"Queue.h"

void QueueInit(Queue* pc)
{
	assert(pc);
	pc->head = pc->tail = NULL;

	pc->size = 0;
}

void QueueDestory(Queue* pc)
{
	assert(pc);
	Queue* cur = pc->head;

	while (cur)
	{
		Queue* next = cur->next;
		free(cur);
		cur = next;
	}
	pc->head = pc->tail = NULL;
	pc->size = 0;
}

void QueuePush(Queue* pc, QDataType x)
{
	QNode* newnode = (Queue*)malloc(sizeof(Queue));
	if (newnode == NULL)
	{
		perror("malloc");
		return;
	}
	newnode->next = x;
	newnode->next = NULL;
	if (pc->head == NULL)
	{
		pc->head = pc->tail = newnode;
	}
	else
	{
		pc->tail->next = newnode;
		pc->tail = newnode;
	}
	pc->size++;
}

void QueuePop(Queue* pc)
{
	assert(pc);
	assert(pc->head != NULL);

	if (pc->head->next == NULL)
	{
		free(pc->head);
		pc->head = pc->tail = NULL;
	}
	else
	{
		Queue* next = pc->head->next;
		free(pc->head);
		pc->head = next;
	}
	pc->size--;
}

void  QueueSize(Queue* pc)
{
	assert(pc);

	return pc->size;
}

bool QueueEmpty(Queue* pc)
{
	assert(pc);

	return pc->size == 0;
}

QDataType QueueFront(Queue* pc)
{
	assert(pc);
	assert(!QueueEmpty(pc));

	return pc->head->data;
}


QDataType QueueBack(Queue* pc)
{
	assert(pc);
	assert(!QueueEmpty(pc));

	return pc->tail->data;
}